<?php

namespace app\index\controller\oceanengine;

use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Oceanengine 控制器
 * Class Oceanengine
 * @package app\index\controller\oceanengine;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class Oceanengine
{
    // 请求参数
    private Request $request;

    private \oceanengine\Oceanengine $oceanengine;

    public function __construct(Request $request, \oceanengine\Oceanengine $oceanengine)
    {
        $this->request = $request;
        $this->oceanengine = $oceanengine;
    }

    /**
     * 巨量引擎API回传
     * @return ApiResponse
     * @url /oceanengine/oceanengine/save
     * @deprecated
     */
    public function save(): ApiResponse
    {
        $request = $this->request;

        $event_type = $request->param('event_type');
        $clickid = $request->param('clickid');

        if (is_blank($event_type)) {
            throw new ValidateException('缺少[event_type]');
        }

        if (is_blank($clickid)) {
            throw new ValidateException('缺少[clickid]');
        }

        $response = $this->oceanengine->conversion($event_type, $clickid);

        if (!$response->isSuccess()) {
            throw new ValidateException('回传失败');
        }

        return ApiResponse::success(null);
    }

}
